package com.toutiao.cut;

import java.util.Properties;

import javax.servlet.Servlet;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

import com.toutiao.cut.impl.CRF;
import com.toutiao.cut.impl.LanguageModel;
import com.toutiao.cut.service.*;

import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.log4j.Logger;


public class SemanticCutterServer {
	
    private static final Logger log = Logger.getLogger(SemanticCutterServer.class);
	
	public static void run(Properties prop){
		
		SemanticCutter cut = new SemanticCutter(prop);
		LanguageModel mCut = cut.getLanaugeModel();
		CRF cCut = cut.getCRF();
		
		int port = Integer.valueOf(prop.getProperty("SERVER_PORT"));
        log.info("run text_cutter on port "+port);
		Server server = new Server(port);
		ServletContextHandler root = new ServletContextHandler(server, "");
		
		Servlet cutServlet = new SemanticCutterServlet(cut);
		Servlet segmentServlet = new SemanticSegmentServlet(cut);
		Servlet mCutServlet = new SentenceCutterServlet(mCut);
		Servlet cCutServlet = new SentenceCutterServlet(cCut);
		Servlet pCutServlet = new PhraseCutterServlet(mCut);

        Servlet processServlet = new DocumentProcessServlet(cut);
		
		root.addServlet(new ServletHolder(cutServlet), "/cut/*");
		root.addServlet(new ServletHolder(segmentServlet), "/segment/*");
		root.addServlet(new ServletHolder(mCutServlet), "/l/*");
		root.addServlet(new ServletHolder(cCutServlet), "/c/*");
		root.addServlet(new ServletHolder(pCutServlet), "/p/*");

        root.addServlet(new ServletHolder(processServlet), "/process/*");
		
		try {
			server.start();
		} catch (Exception e){
			e.printStackTrace();
		}
		
	}

    public static void runThriftServer(Properties prop){

        SemanticCutter cut = new SemanticCutter(prop);

        TProcessor tp = new DocumentProcess.Processor<
                DocumentProcess.Iface>(
                    new DocumentProcessImpl(cut));
        int port = Integer.valueOf(prop.getProperty("SERVER_PORT"));
        log.info("run text_cutter on port "+port);
        try {
            TServerSocket ss = new TServerSocket(new TServerSocket.ServerSocketTransportArgs().port(port).backlog(1024));
            TThreadPoolServer.Args tArgs = new TThreadPoolServer.Args(ss);
            tArgs.processor(tp);
            tArgs.protocolFactory(new TBinaryProtocol.Factory());
            tArgs.maxWorkerThreads(160);

            TServer server = new TThreadPoolServer(tArgs);

            server.serve();

        } catch (Exception e){
            e.printStackTrace();
        }
    }
    
    
    public static void runThriftServer2(Properties prop) {
    	
    	SemanticCutter cut = new SemanticCutter(prop);
    	
    	TProcessor tp = new MultiDocumentProcess.Processor<
    			MultiDocumentProcess.Iface>(
    				new MultiDocumentProcessImpl(cut));
    	int port = Integer.valueOf(prop.getProperty("SERVER_PORT"));
    	log.info("run text_cutter on port "+port);
    	try {
            TServerSocket ss = new TServerSocket(new TServerSocket.ServerSocketTransportArgs().port(port).backlog(1024));
            TThreadPoolServer.Args tArgs = new TThreadPoolServer.Args(ss);
            tArgs.processor(tp);
            tArgs.protocolFactory(new TBinaryProtocol.Factory());
            tArgs.maxWorkerThreads(160);

            TServer server = new TThreadPoolServer(tArgs);

            server.serve();
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    	
    	
    }
}

